package org.jeecg.modules.inspect.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.inspect.entity.InsCard;
import org.jeecg.modules.inspect.entity.InsPoint;
import org.jeecg.modules.inspect.service.IInsCardService;
import org.jeecg.modules.inspect.service.IInsPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 点位信息
 * @Author: jeecg-boot
 * @Date:   2023-11-23
 * @Version: V1.0
 */
@Api(tags="点位信息")
@RestController
@RequestMapping("/inspect/insPoint")
@Slf4j
public class InsPointController extends JeecgController<InsPoint, IInsPointService> {
	@Autowired
	private IInsPointService insPointService;
	 @Autowired
	private IInsCardService insCardService;

	/**
	 * 分页列表查询
	 *
	 * @param insPoint
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "点位信息-分页列表查询")
	@ApiOperation(value="点位信息-分页列表查询", notes="点位信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<InsPoint>> queryPageList(InsPoint insPoint,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<InsPoint> queryWrapper = QueryGenerator.initQueryWrapper(insPoint, req.getParameterMap());
		Page<InsPoint> page = new Page<InsPoint>(pageNo, pageSize);
		IPage<InsPoint> pageList = insPointService.page(page, queryWrapper);
		List<InsPoint> records = pageList.getRecords();
		records.stream().forEach(item -> {
			InsCard insCard = insCardService.getById(item.getCardId());
			item.setCardNo(insCard.getCardNo());
			item.setCardName(insCard.getCardName());
		});

		return Result.OK(pageList);
	}

	/**
	 * 通过cardNo查询
	 *
	 * @param cardNo
	 * @return
	 */
	//@AutoLog(value = "点位信息-通过cardNo查询")
	@ApiOperation(value="点位信息-通过cardNo查询", notes="点位信息-通过cardNo查询")
	@GetMapping(value = "/queryByCardNo")
	public Result<InsPoint> queryByCardNo(@RequestParam(name="cardNo",required=true) String cardNo) {
		InsCard insCard = insCardService.getOne(Wrappers.<InsCard>lambdaQuery().eq(InsCard::getCardNo, cardNo));
		if(null == insCard) {
			return Result.error("卡信息不存在！");
		}
		List<InsPoint> insPointList = insPointService.list(Wrappers.<InsPoint>lambdaQuery().eq(InsPoint::getCardId, insCard.getId()));
		if(CollUtil.isEmpty(insPointList)) {
			return Result.error("未找到对应点位数据！");
		}
		insPointList.get(0).setCardName(insCard.getCardName());

		return Result.OK(insPointList.get(0));
	}

	/**
	 *   添加
	 *
	 * @param insPoint
	 * @return
	 */
	@AutoLog(value = "点位信息-添加")
	@ApiOperation(value="点位信息-添加", notes="点位信息-添加")
	//@RequiresPermissions("org.jeecg.modules:ins_point:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody InsPoint insPoint) {
		InsCard insCard = insCardService.getById(insPoint.getCardId());
		// 查询卡是否已被绑定
		List<InsPoint> insPoints = insPointService.list(Wrappers.<InsPoint>lambdaQuery().eq(InsPoint::getCardId, insPoint.getCardId()));
		if(CollUtil.isNotEmpty(insPoints)) {
			return Result.error("卡号：" + insCard.getCardName() + "，已被其他点位绑定！");
		}
		insPoint.setCardNo(insCard.getCardNo());

		insPointService.save(insPoint);

		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param insPoint
	 * @return
	 */
	@AutoLog(value = "点位信息-编辑")
	@ApiOperation(value="点位信息-编辑", notes="点位信息-编辑")
	//@RequiresPermissions("org.jeecg.modules:ins_point:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody InsPoint insPoint) {
		// 原点位信息
		InsPoint oldPoint = insPointService.getById(insPoint.getId());
		InsCard insCard = insCardService.getById(insPoint.getCardId());


		if(!oldPoint.getCardId().equals(insPoint.getCardId())) {
			// 查询卡是否已被绑定
			List<InsPoint> insPoints = insPointService.list(Wrappers.<InsPoint>lambdaQuery().eq(InsPoint::getCardId, insPoint.getCardId()));
			if(CollUtil.isNotEmpty(insPoints)) {
				return Result.error("卡号：" + insCard.getCardName() + "，已被其他点位绑定！");
			}
		}
		insPoint.setCardNo(insCard.getCardNo());

		insPointService.updateById(insPoint);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "点位信息-通过id删除")
	@ApiOperation(value="点位信息-通过id删除", notes="点位信息-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:ins_point:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		insPointService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "点位信息-批量删除")
	@ApiOperation(value="点位信息-批量删除", notes="点位信息-批量删除")
	//@RequiresPermissions("org.jeecg.modules:ins_point:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.insPointService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "点位信息-通过id查询")
	@ApiOperation(value="点位信息-通过id查询", notes="点位信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<InsPoint> queryById(@RequestParam(name="id",required=true) String id) {
		InsPoint insPoint = insPointService.getById(id);
		if(insPoint==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(insPoint);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param insPoint
    */
    //@RequiresPermissions("org.jeecg.modules:ins_point:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, InsPoint insPoint) {
        return super.exportXls(request, insPoint, InsPoint.class, "点位信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("ins_point:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, InsPoint.class);
    }

}
